<template>
    <div>
        <div class="sign-in clearfit">
            <span v-if="!land" class="l">未登录</span>
            <span v-if="!land" @click="signIn" class="btn-sign r">登录</span>
            <span v-if="land" class="l">{{userName}}</span>
            <span v-if="land" @click="signOut" class="btn-sign r">退出登录</span>
        </div>
        <div class="reading-before">
            <div class="title">最近阅读</div>
            <ul>
                <li v-if="!land">还木有任何书籍(～￣▽￣)～</li>
                <li 
                    class="book-item"
                    v-for="item,index in bookList"
                    :key="item+index"
                    @click="gotoBookDetail(item)">
                    {{item}}
                </li>
            </ul>
        </div>
    </div>
</template>

<script>
export default {
    name: 'myBooks',
    props: {
        userName: String,
        land: Boolean,
    },
    data() {
        return {
            bookList: []
        }
    },
    methods: {
        signOut() {
            this.bookList = [];
            this.$emit('zi-signOut');
        },
        signIn() {
            this.getList();
            this.$emit('zi-signIn');
        },
        getList() {
            this.$axios.get('./static/bookself.json').then((success)=> {
                this.bookList = success.data.bookList;
            },(err)=> {
                console.log(err);
            })
        },
        gotoBookDetail(str) {
            this.$router.push({name:"bookDetail",query:{bookName:str}});
        }
    },
    mounted() {
        if(this.land){
            this.getList();
        }
    }
}
</script>

<style lang="scss" scoped>
.sign-in {
    padding: 8px;
    font-size: 16px;
    line-height: 30px;
    .btn-sign {
        padding: 0 10px;
        height: 30px;
        text-align: center;
        color: white;
        background-color: #FF4644;
    }
}
.reading-before {
    color: #FF4644;
    text-align: center;
    font-size: 16px;
    .title {
        height: 30px;
        line-height: 30px;
        background-color: #F2F2F2;
        border-bottom: 5px solid #FF4644;
    }
    ul {
        min-height: 200px;
        border-bottom: 2px solid#ccc;
    }
    .book-item {
        text-align: left;
        color: #333;
        padding: 10px;
        border: 1px solid #ccc;
    }
}
</style>